'''
Created on 2010-4-11

@author: chengmeng
'''
import wx

class Plugin:
	
	def __init__(self,frame):
		ID_Undo = wx.NewId()
		ID_Redo = wx.NewId()
		ID_Clear = wx.NewId()
		
		import environment
		
		menu = wx.Menu()
		self.undoItem = menu.Append(ID_Undo, "&Undo\tCtrl-Z")
		self.redoItem = menu.Append(ID_Redo, "&Redo\tCtrl-Y")
		if environment.isDebug :
			menu.Append(ID_Clear, "Clear")

		frame.GetMenuBar().Insert(1,menu, "&Edit")
		
		frame.Bind(wx.EVT_MENU, self._Undo, id=ID_Undo)
		frame.Bind(wx.EVT_MENU, self._Redo, id=ID_Redo)
		if environment.isDebug :
			frame.Bind(wx.EVT_MENU, self.clear, id=ID_Clear)
		
		
		self.undoItem.Enable(False)
		self.redoItem.Enable(False)
		
		self.doList = []
		self.currDoIndex = -1
		pass

	def _Undo(self,e):
		if self.currDoIndex > -1 :
			self.doList[self.currDoIndex].undo()
			self.currDoIndex -= 1
			
			self.redoItem.Enable(True)
			if self.currDoIndex == -1 :
				self.undoItem.Enable(False)
		pass

	def _Redo(self,e):
		if len(self.doList)-1 > self.currDoIndex :
			self.currDoIndex += 1
			self.doList[self.currDoIndex].redo()
			
			self.undoItem.Enable(True)
			if len(self.doList)-1 == self.currDoIndex :
				self.redoItem.Enable(False)
		pass

#	def _KeyUpEventListener(self, event):
#		pass

	def addDo(self, do):
		while len(self.doList)-1 > self.currDoIndex:
			self.doList.pop()

		self.doList.append(do)
		self.currDoIndex += 1
		self.undoItem.Enable(True)
		self.redoItem.Enable(False)
		pass

	def clear(self,e = None):
		self.doList = []
		self.currDoIndex = -1
		self.undoItem.Enable(False)
		self.redoItem.Enable(False)
		pass

	def IBeginLoadScene(self):
		self.clear()
		pass
	
	def IClearScene(self):
		self.clear()
		pass
	
##
def IInit(frame):
	return Plugin(frame)